Skip to content

Forward AI agent session/trace ids into outbound request headers#268

Open
sdairs wants to merge 1 commit into
mainfrom
issue-267-forward-agent-session-headers
Open

Forward AI agent session/trace ids into outbound request headers#268
sdairs wants to merge 1 commit into
mainfrom
issue-267-forward-agent-session-headers

Conversation

@sdairs

@sdairs sdairs commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

Closes #267

What

Propagate the calling AI agent's session id and traceparent into outbound request headers, so backend telemetry can correlate every clickhousectl call made within a single agent session/conversation.

Header names the backend looks for:

  • agent-session-idAgent::session_id
  • traceparentAgent::traceparent (raw W3C value)

How

  • Bump is-ai-agent 0.2.10.4.0 for the new Agent::session_id / Agent::traceparent fields.
  • New src/http.rs module — the canonical place to construct outbound reqwest clients. It owns client_builder() (pre-applies the User-Agent and the agent default headers) and agent_headers(). user_agent.rs keeps its narrow job of building just the User-Agent string.
  • Route all reqwest::Client::builder() sites through crate::http::client_builder() — not just the Cloud API client, but also OAuth, the self-updater, and the version manager (they all hit a clickhouse.com property in some way). This makes the builder the single source of truth, so future builders inherit the headers automatically.
  • No clickhouse-cloud-api change needed: its client is built from the reqwest::Client we hand it in CloudClient::new(), and default headers ride along on every request (management API + SQL query paths).
  • Header values are guarded with HeaderValue::from_str(..).ok() since session ids are opaque vendor strings.

Tests

  • Pure-logic unit tests for the header builder in http.rs (both ids, neither, each alone, invalid value dropped, builder smoke).
  • Wiremock subprocess test (agent_session_and_trace_headers_are_forwarded) asserting both headers land on the wire when running under a detected agent.

cargo test -p clickhousectl and cargo clippy --all-targets are clean.

🤖 Generated with Claude Code


Note

Low Risk
Additive telemetry headers on outbound HTTP with no auth or request-body changes; behavior is unchanged when no agent is detected.

Overview
When clickhousectl runs under a detected coding agent, outbound HTTP now carries agent-session-id and traceparent (from is-ai-agent 0.4.0) so backend telemetry can tie CLI calls to one agent run.

A new http module exposes client_builder(), which applies the existing User-Agent plus those default headers. Cloud OAuth, the Cloud API client, self-update, and version-manager downloads/probes all build clients through it instead of ad hoc reqwest::Client::builder() calls.

Invalid header values are skipped rather than failing the client build. Coverage includes unit tests for header construction and a wiremock subprocess test that asserts both headers on a real cloud org list request when agent env vars are set.

Reviewed by Cursor Bugbot for commit 8e969f1. Bugbot is set up for automated code reviews on this repo. Configure here.

Detect the calling AI agent's session id and W3C traceparent (via
is-ai-agent 0.4.0) and forward them as `agent-session-id` and
`traceparent` request headers on every outbound HTTP client, so backend
telemetry can correlate a single agent session's calls.

- Bump is-ai-agent 0.2.1 -> 0.4.0.
- Add src/http.rs: the canonical reqwest client factory
  (client_builder) plus agent_headers(); user_agent.rs keeps its narrow
  job of building the User-Agent string.
- Route all reqwest::Client::builder() sites (Cloud client, OAuth,
  updater, version manager) through crate::http::client_builder() so
  coverage is uniform and future builders inherit it. No
  clickhouse-cloud-api change: default headers ride the client we hand it.
- Tests: pure-logic header-builder unit tests + a wiremock subprocess
  test asserting both headers land on the wire.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@sdairs sdairs requested a review from iskakaushik as a code owner June 19, 2026 17:39
@sdairs sdairs deployed to cloud-integration June 19, 2026 17:39 — with GitHub Actions Active
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forward AI agent session/trace id into Cloud API request headers

1 participant